﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SDPChecker
{
    public class StableDependenciesPrincipleChecker
    {
        private readonly IDictionary<string, AssemblyInfo> _instableAssemblies;
        private IEnumerable<AssemblyInfo> _dependencies;
 
        public StableDependenciesPrincipleChecker(IEnumerable<AssemblyInfo> instableAssemblies, IEnumerable<AssemblyInfo> dependencies)
        {
            _instableAssemblies = CreateDictionary(instableAssemblies);
            _dependencies = CreateDependenciesWithInstability(dependencies);
        }

        private IEnumerable<AssemblyInfo> CreateDependenciesWithInstability(IEnumerable<AssemblyInfo> dependencies)
        {
            return dependencies.Select(info => info.CreateCopyWithInstabilityValues(_instableAssemblies));
        }

        private IDictionary<string, AssemblyInfo> CreateDictionary(IEnumerable<AssemblyInfo> instableAssemblies)
        {
            var dictionary = new Dictionary<string, AssemblyInfo>();
            instableAssemblies.ToList().ForEach(info => dictionary.Add(info.Name, info));
            return new ReadOnlyDictionary<string, AssemblyInfo>(dictionary);
        }

        public bool BadBoysFound
        {
            get { return _dependencies.Any(dependency => dependency.IsBadBoy); }
        }

        public IEnumerable<AssemblyInfo> BadBoys
        {
            get
            {
                return _dependencies.Where(dependency => dependency.IsBadBoy);
            }
        }
    }
}
